#include "DTIFiberTrackingCPU.h"

char *pMaskPathname = NULL;
char *pDTIPathname = NULL;
char *pFiberPathname = NULL;
float stepSize = 0.1f;
float maxLength = 50.0f;
float faThreshold = 0.001f;
float angleThreshold = 0.707f;
int scale = 1;

CDTIFiberTrackingCPU *pDTIFiberTrackingCPU = NULL;


void PrintUsage()
{
	printf("Usage: ");
	printf("CPU_DTIFiberTracking -mask <mask> -dti <DTI> -fiber <Fiber> [-stepsize <stepsize> -maxlength <maxlength> -fa_thresold <fa_threshold> -angle_threshold <fa_threshold> -scale <scale>]\n");
	printf("\n");
	printf("-mask <Mask> = brain mask nrrd file\n");
	printf("-dti <DTI> = input DTI nrrd file\n");
	printf("-fiber <Fiber> = output Fiber configure file\n");
	printf("-stepsize <stepsize> = step size\n");
	printf("-maxlength <maxlength> = maximum fiber length\n");
	printf("-fa_threshold <fa_thresold> = FA threshold used to differ white matter and grey matter\n");
	printf("-angle_threshold <angle_thresold> = angle threshold between two directions, in cosine\n");
	printf("-scale <scale> = scale of the result\n");
}

int ParseArguments(int argc, char *argv[])
{
	for (int i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "-mask") == 0) {
			pMaskPathname = argv[++i];
		} else if (strcmp(argv[i], "-dti") == 0) {
			pDTIPathname = argv[++i];
		} else if (strcmp(argv[i], "-fiber") == 0) {
			pFiberPathname = argv[++i];
		} else if (strcmp(argv[i], "-stepsize") == 0) {
			stepSize = (float)atof(argv[++i]);
		} else if (strcmp(argv[i], "-maxlength") == 0) {
			maxLength = (float)atof(argv[++i]);
		} else if (strcmp(argv[i], "-fa_threshold") == 0) {
			faThreshold = (float)atof(argv[++i]);
		} else if (strcmp(argv[i], "-angle_threshold") == 0) {
			angleThreshold = (float)atof(argv[++i]);
		} else if (strcmp(argv[i], "-scale") == 0) {
			scale = (uint)atoi(argv[++i]);
		} else {
            printf("unknown %s\n", argv[i]);
			return 1;		// error
		}
	}

	if (pMaskPathname && pDTIPathname && pFiberPathname) {
		return 0;
	} else {
		return 1;
	}
}


int main(int argc, char *argv[])
{
	if (ParseArguments(argc, argv) != 0) {
		PrintUsage();
		return 0;
	}

	pDTIFiberTrackingCPU = new CDTIFiberTrackingCPU();
	pDTIFiberTrackingCPU->InitDTIFiberTracking(pDTIPathname, pMaskPathname, 
		stepSize, maxLength, faThreshold, angleThreshold);

	pDTIFiberTrackingCPU->SetTENDParameters(0.0f, 1.0f);
	pDTIFiberTrackingCPU->SetMLSParameters(2.0f);
	pDTIFiberTrackingCPU->FiberTracking(FT_TEND_MLS, pFiberPathname, scale);

	return 0;
}
